package info.openlvb.openface.client.base.person;

import java.util.Iterator;
import java.util.List;

import info.openlvb.openface.client.base.Account;
import info.openlvb.openface.client.base.Group;
import info.openlvb.openface.client.service.LdapService;
import info.openlvb.openface.client.service.LdapServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.event.GridRowListener;

public class ListPersonPanel extends VerticalPanel{

	Window window;
	Store store = null;
	RecordDef recordDef;
	GridPanel grid;
	
	private final LdapServiceAsync ldapService = GWT.create(LdapService.class);
	
	public ListPersonPanel(){
		
		recordDef = new RecordDef(  
				new FieldDef[]{  
						new StringFieldDef("pseudo"),
						new StringFieldDef("prenom"),
						new StringFieldDef("nom"),
						new StringFieldDef("mail"),
						new StringFieldDef("groupe"),
						new StringFieldDef("rep_access")
				}  
		);
		  
		store = new Store(recordDef);
		ldapService.searchUsers(new AsyncCallback<List<Account>>() {
			public void onSuccess(List<Account> result) {
				for (Iterator<?> iterator = result.iterator();iterator.hasNext();) {
					Account cour = (Account) iterator.next();
					Record r = recordDef.createRecord(new Object[]{
							cour.getLogin(),cour.getPrenom(),cour.getNom(),cour.getMail(),cour.getGroupe_principal(),cour.getHome()
					});
					store.insert(0,r);
					store.commitChanges();
				}
				
			}
			public void onFailure(Throwable caught) {
				MessageBox.alert("probleme", "probleme de communication avec le backend");
			}
		});
		ColumnConfig[] columns = new ColumnConfig[]{  
				//column ID is company which is later used in setAutoExpandColumn  
				new ColumnConfig("Pseudo", "pseudo", 120,true),
				new ColumnConfig("Nom", "nom", 75,true),
				new ColumnConfig("Prenom", "prenom", 100,true),
				new ColumnConfig("Mail", "mail", 200,true),
				new ColumnConfig("Groupe Principal", "groupe", 100,true),
				new ColumnConfig("Répertoire perso", "rep_access", 200,true)
		};  
		ColumnModel columnModel = new ColumnModel(columns);  

		grid = new GridPanel();  
		grid.setStore(store);  
		grid.setColumnModel(columnModel);  

		grid.setFrame(true);  
		grid.setStripeRows(true);  
		grid.setWidth(850);  
		grid.setHeight(300);  
		grid.setTitle("Liste des utilisateurs");  
			
		grid.addGridRowListener(new GridRowListener() {
			public void onRowClick(GridPanel grid, int rowIndex, EventObject e) {
				Record record = grid.getStore().getRecordAt(rowIndex);
				new ModifyAccountWindow(ListPersonPanel.this,record);	            
			}
			public void onRowContextMenu(GridPanel grid, int rowIndex,
					EventObject e) {
			}
			public void onRowDblClick(GridPanel grid, int rowIndex,
					EventObject e) {
			}
			
		});

		add(grid);  
	}
	
	/**
	 * pour rafraichir automatiquement la liste des groupes
	 */
	public void refresh() {
		store.removeAll();
		ldapService.searchUsers(new AsyncCallback<List<Account>>() {
			public void onFailure(Throwable caught) {
				MessageBox.alert("probleme", "probleme de communication avec le backend");
			}
			public void onSuccess(List<Account> result) {
				for (Iterator<?> iterator = result.iterator();iterator.hasNext();) {
					Account cour = (Account) iterator.next();
					Record r = recordDef.createRecord(new Object[]{
							cour.getLogin(),cour.getPrenom(),cour.getNom(),cour.getMail(),cour.getGroupe_principal(),cour.getHome()
					});
					store.insert(0,r);
					store.commitChanges();
				}
			}			
		});

	}
	
}
